// pages/my/todayEat/todayEat.js
Page({
    data: {
        active:'select',
        presetList:[
            {
                id:'1',
                checked:true,
                name:'豆浆油条',
                breakfast:true,
                lunch:false,
                afternoonTea:false,
                dinner:true,
                midnightSnack:false
            },
            {
                id:'2',
                checked:true,
                name:'咸菜粥',
                breakfast:true,
                lunch:false,
                afternoonTea:false,
                dinner:false,
                midnightSnack:false
            },
            {
                id:'3',
                checked:true,
                name:'回锅肉',
                breakfast:false,
                lunch:true,
                afternoonTea:false,
                dinner:true,
                midnightSnack:false
            },
            {
                id:'4',
                checked:true,
                name:'酸菜鱼',
                breakfast:false,
                lunch:true,
                afternoonTea:false,
                dinner:true,
                midnightSnack:false
            },
            {
                id:'5',
                checked:true,
                name:'炒面',
                breakfast:false,
                lunch:true,
                afternoonTea:false,
                dinner:true,
                midnightSnack:false
            },
            {
                id:'6',
                checked:true,
                name:'小龙虾',
                breakfast:false,
                lunch:true,
                afternoonTea:false,
                dinner:true,
                midnightSnack:true
            }
        ]
    },
    onShow(){
        let presetList=wx.getStorageSync('presetList')
        if(presetList!==''&&presetList!==undefined&&presetList!==null){
            this.setData({
                presetList:JSON.parse(presetList)
            })
        }
    },
    onChange(e){
        this.setData({
            active:e.detail.name
        })
    },
    onClickDeletePreset(e){
        let id=e.detail.id;
        let presetList=this.data.presetList.filter(item=>{
            return item.id!==id
        })
        this.setData({
            presetList:presetList
        })
        wx.setStorage({
            key:'presetList',
            data:JSON.stringify(presetList)
        })
    },
    onChangeCheckedPreset(e){
        let id=e.detail.id;
        let checked=e.detail.checked;
        let presetList=JSON.parse(JSON.stringify(this.data.presetList));
        let index=presetList.findIndex(item=>item.id===id);
        if(index!==-1){
            presetList[index].checked=checked;
            this.setData({
                presetList:presetList
            })
            wx.setStorage({
                key:'presetList',
                data:JSON.stringify(presetList)
            })
        }
    },
    onSavePreset(e){
        let presetList=JSON.parse(JSON.stringify(this.data.presetList));
        presetList.push(e.detail)
        this.setData({
            presetList:presetList
        })
        wx.setStorage({
            key:'presetList',
            data:JSON.stringify(presetList)
        })
    }
})